package com.easy.life.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.easy.life.common.R;
import com.easy.life.redis.impl.StringRedisServiceImpl;
import com.easy.life.utils.JwtUtils;
import com.easy.life.utils.MD5Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;


import com.easy.life.dao.UserDao;
import com.easy.life.entity.UserEntity;
import com.easy.life.service.UserService;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.HashMap;


@Service
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {

    @Resource
    private UserDao userDao;
    @Autowired
    private StringRedisServiceImpl stringRedisService;


    @Override
    public R login(String username, String password) {
        UserEntity user = userDao.selectOne(new QueryWrapper<UserEntity>().eq("username", username));
        if (user!=null&&user.getPassword().equals(MD5Utils.code(password))){
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put("username",user.getUsername());
            hashMap.put("email",user.getEmail());
            hashMap.put("avatar",user.getAvatar());
            hashMap.put("mobile",user.getMobile());
            HashMap<String, String> map = new HashMap<>();
            map.put("username",user.getUsername());
            map.put("password",user.getPassword());
            String token= JwtUtils.getToken(map);
            hashMap.put("token",token);
            hashMap.put("id",user.getId());
            user.setToken(token);
            userDao.updateById(user);
            return R.ok().put("data",hashMap);
        }else {
            return R.error(400,"用户或密码错误！");
        }

    }

    @Override
    public R register(String username, String password,String phone,String code) {
        Boolean isPhone = stringRedisService.hasKey(phone);
        if (isPhone&&stringRedisService.get(phone).equals(code)){
            UserEntity userEntity=new UserEntity();
            userEntity.setUsername(username);
            userEntity.setMobile(phone);
            userEntity.setPassword(MD5Utils.code(password));
            userDao.insert(userEntity);
            return R.ok("注册成功");
        }else {
            return R.error(404,"验证码错误");
        }
    }

    @Override
    public R isUser(String username) {
        UserEntity user = userDao.selectOne(new QueryWrapper<UserEntity>().eq("username", username));
        if (user==null){
            return R.ok().put("exist",false);
        }else{
            return R.ok().put("exist",true);
        }
    }

    @Override
    public Boolean isPhone(String phone) {
        UserEntity user = userDao.selectOne(new QueryWrapper<UserEntity>().eq("mobile",phone ));
        if (user==null){
            return false;
        }else{
            return true;
        }
    }
}